package com.equipment;

import java.util.NoSuchElementException;

import android.util.Log;

import com.util.EncodingUtil;

/**
 * B5读系统信息响应
 * @version 1.18
 * @author 刘亚东
 *
 */
public class ReadSystemResponse {
	public static void main(String[] args) {
		String str = "55AA070068DFB5006563B08F6C900000002780200710012050010100000000000000000000007370BEDA664EDD7EE25ED81CA1242F00000000000000000002112233445566778899010203880000000003FFFFFF122200C3505CBE77F4DB3ED72FF361FBD075F7009118011902020D";
		ReadSystemResponse rst = new ReadSystemResponse();
		System.out.println(rst.analysis(str));
	}

	/**
	 * 测试
	 */
	public String toString() {
		return stx + rsctl + code + subCode + bcc + "内容：" + status + sysInfoLen
				+ sysInfoData;
	}
	// 帧开始标记
	private String stx = "55AA";
	// 串口帧序列号
	private String rsctl = "00";
	// 长度
	private String Length = "";
	// 代码类型
	private String code = "DF";
	// 拓展子码
	private String subCode = "B5";
	// 读系统响应状态
	private String status = "00";
	// 系统信息长度
	private String sysInfoLen = "19";
	// 系统信息内容
	private String sysInfoData = "";
	// 合同序列号8字节
	private String contractNumber = "";
	// 合同签署日期4字节
	private String signDate = "";
	// 合同过期日期4字节
	private String dueDate = "";
	// 拆卸状态1字节
	private String dismantle = "";
	// 异或校验值
	private String bcc = "6c";
	// OBU密钥版本
	private String obuVe = "";

	/**
	 * 解析B5读系统信息响应
	 *
	 * @param context
	 * @return
	 */
	public ReadSystemResponse analysis(String context)
			throws NoSuchElementException {
		ReadSystemResponse rr = null;
		try {
			// 判断是否是B5指令
			if (!context.substring(12, 14).toUpperCase().equals("B5")) {
				throw new NoSuchElementException("读系统错误");
			}
			// 判断状态码
			if (!context.substring(14, 16).equals("00")) {
				throw new NoSuchElementException("读系统错误");
			}
			// 截取到BCC校验值 需要判断校验是否相等
			bcc = context.substring(context.length() - 2).toUpperCase();
			// 截取到发送的数据(总长度-2个字节计算到数据的最后位置)
			String str = context.substring(4, context.length() - 2);
			// 得到比对校验
			str = EncodingUtil.yihuo(EncodingUtil.change(str)).toUpperCase();
			// 判断校验是否一致
			if (!str.equals(bcc)) {
				throw new NoSuchElementException("读系统异或错误");
			}
			// 如果没问题就new 对象 把解析的值放入对象
			rr = new ReadSystemResponse();
			// 解析串口帧序列号
			rsctl = context.substring(4, 6);
			rr.setRsctl(rsctl);
			// 解析长度
			Length = context.substring(6, 10);
			rr.setLength(Length);
			// 解析代码类型
			code = context.substring(10, 12);
			rr.setCode(code);
			// 解析拓展子代码
			subCode = context.substring(12, 14);
			rr.setSubCode(subCode);
			// 解析读系统响应状态
			status = context.substring(14, 16);
			rr.setStatus(status);
			// 解析系统信息长度
			int leng = Integer.parseInt(EncodingUtil.sixToTen(context
					.substring(16, 18)));
			sysInfoLen = context.substring(16, 18);
			rr.setSysInfoLen(sysInfoLen);
			leng = (leng * 2) + 18;
			// 解析系统信息内容
			sysInfoData = context.substring(18, leng);
			rr.setSysInfoData(sysInfoData);

		} catch (Exception e) {
			e.printStackTrace();
			throw new NoSuchElementException("读系统错误");
		}
		return rr;
	}

	/**
	 * 截取系统信息中需要的字段 27
	 *
	 * @param systemInfo
	 */
	public void analysisSystemInfo(String systemInfo) {
		Log.e("系统信息：", systemInfo);
		// OBU密钥版本
		this.obuVe = systemInfo.substring(18, 20);
		Log.e("系统信息：", obuVe);
		// 得到合同序列号
		this.contractNumber = systemInfo.substring(20, 36);
		// 得到合同签署日期
		this.signDate = systemInfo.substring(36, 44);
		// 得到合同过期日期
		this.dueDate = systemInfo.substring(44, 52);
		// 得到拆卸状态
		this.dismantle = systemInfo.substring(52, 54);
	}

	public String getContractNumber() {
		return contractNumber;
	}

	public void setContractNumber(String contractNumber) {
		this.contractNumber = contractNumber;
	}

	public String getSignDate() {
		return signDate;
	}

	public void setSignDate(String signDate) {
		this.signDate = signDate;
	}

	public String getDueDate() {
		return dueDate;
	}

	public void setDueDate(String dueDate) {
		this.dueDate = dueDate;
	}

	public String getDismantle() {
		return dismantle;
	}

	public void setDismantle(String dismantle) {
		this.dismantle = dismantle;
	}

	public String getStx() {
		return stx;
	}

	public void setStx(String stx) {
		this.stx = stx;
	}

	public String getRsctl() {
		return rsctl;
	}

	public void setRsctl(String rsctl) {
		this.rsctl = rsctl;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getSubCode() {
		return subCode;
	}

	public void setSubCode(String subCode) {
		this.subCode = subCode;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getSysInfoLen() {
		return sysInfoLen;
	}

	public void setSysInfoLen(String sysInfoLen) {
		this.sysInfoLen = sysInfoLen;
	}

	public String getSysInfoData() {
		return sysInfoData;
	}

	public void setSysInfoData(String sysInfoData) {
		this.sysInfoData = sysInfoData;
	}

	public String getBcc() {
		return bcc;
	}

	public void setBcc(String bcc) {
		this.bcc = bcc;
	}

	public String getLength() {
		return Length;
	}

	public void setLength(String length) {
		Length = length;
	}

	public String getObuVe() {
		return obuVe;
	}

	public void setObuVe(String obuVe) {
		this.obuVe = obuVe;
	}

}
